package com.telecom.restaurant.controllor.manage;

import com.telecom.restaurant.constant.DataResultConstant;
import com.telecom.restaurant.constant.ManageThreadLocal;
import com.telecom.restaurant.dao.FoodDao;
import com.telecom.restaurant.domin.DataResult;
import com.telecom.restaurant.domin.FoodSearchResult;
import com.telecom.restaurant.domin.PaginationResult;
import com.telecom.restaurant.model.FoodPO;
import com.telecom.restaurant.model.param.FoodSearchParam;
import com.telecom.restaurant.service.PictureService;
import com.telecom.restaurant.service.FoodService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import static com.telecom.restaurant.constant.DataResultConstant.SAME_FOOD_NAME;

/**
 * Created by qiqi on 2019/12/3.
 */
@Controller
@RequestMapping("/restaurant/food")
public class FoodController {

    Logger logger = LoggerFactory.getLogger(FoodController.class);

    @Autowired
    FoodDao foodDao;

    @Autowired
    private FoodService foodService;

    @Autowired
    private PictureService pictureService;

    /**
     * 查询单个菜品信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/getInfo")
    @ResponseBody
    public DataResult getInfo(Long id) {
        if (id == null) {
            return DataResultConstant.WRONG_PARAM;
        }

        FoodPO foodPO = foodDao.selectByPrimaryKey(id);
        pictureService.renderFoodPicture(foodPO);
        DataResult dataResult = DataResultConstant.buildSuccessResult(foodPO);
        return dataResult;
    }


    @RequestMapping("/getFoodList")
    @ResponseBody
    public DataResult getFoodList(Integer type, Integer pageSize, Integer pageNo) {
        if (type == null) {
            type = 1;
        }

        FoodSearchParam param = new FoodSearchParam();
        param.setPageNo(pageNo);
        param.setPageSize(pageSize);
        param.setType(type);
        Long restaurantId = ManageThreadLocal.restaurantId.get();
        param.setRestaurantId(restaurantId);

        PaginationResult result = foodService.getFoodList(param);

        pictureService.renderFoodListPicture(result.getDataList());

        DataResult dataResult = DataResultConstant.buildSuccessResult(result);
        return dataResult;
    }

    @RequestMapping("/search")
    @ResponseBody
    public DataResult searchFood(String keyword) {

        if (StringUtils.isBlank(keyword)) {
            keyword = null;
        }
        FoodSearchParam param = new FoodSearchParam();
        param.setKeyword(keyword);
        Long restaurantId = ManageThreadLocal.restaurantId.get();
        param.setRestaurantId(restaurantId);

        FoodSearchResult result = foodService.searchFood(param);
        pictureService.renderFoodListPicture(result.getPackageDish());
        pictureService.renderFoodListPicture(result.getPackageMain());
        pictureService.renderFoodListPicture(result.getOtherMain());
        pictureService.renderFoodListPicture(result.getOtherDish());

        DataResult dataResult = DataResultConstant.buildSuccessResult(result);
        return dataResult;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public DataResult save(FoodPO food) {

        Long restaurantId = ManageThreadLocal.restaurantId.get();
        food.setRestaurantId(restaurantId);

        if (food.getId() == null) {
            try {
                foodDao.insert(food);
            } catch (DuplicateKeyException e) {
                logger.error("isnert food error. {}", food, e);
                return SAME_FOOD_NAME;
            }
        } else {
            foodDao.updateByPrimaryKey(food);
        }
        pictureService.renderFoodPicture(food);
        DataResult dataResult = DataResultConstant.buildSuccessResult(food);
        return dataResult;
    }
}
